package com.thinking.stormStudy;

import java.util.Map;
import java.util.Random;

import org.apache.log4j.spi.LoggerFactory;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;
import org.apache.storm.utils.Utils;
import org.slf4j.Logger;


/**
 * 测试 storm 统计使用量的
 * @author tangsixiang
 *
 */
public class TestWordSpout extends BaseRichSpout {
  static 	  Logger  logger =org.slf4j.LoggerFactory.getLogger(TestWordSpout.class);
	SpoutOutputCollector _collector;
	

	public void open(Map cong, TopologyContext  context, SpoutOutputCollector collector) {
       _collector=collector;
	}

	 private static  int lengh=0;
	 /**
	  * 系统spout 循环的自动发射随机字符串
	  */
	final static  String[] words=new String[]{"spring","mybatis","jdbc","mysql","serlvet","jsp","kafka","zookeeper"};
	public void nextTuple() {
     	Utils.sleep(1000);
     	
     	if(words.length<=lengh){lengh=0;}
     	logger.info("发送的字符串为：{}",words[lengh]);
     	_collector.emit(new Values(words[lengh]));
     	lengh++;

	}
	
	
	@Override
	public void declareOutputFields(OutputFieldsDeclarer declarer) {
		/**
		 * 设置一个向下传递的key
		 */
		declarer.declare(new Fields("word"));
	}

}
